package cs342.group1.messenger;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

public class PasswordHasher {
	//this method will generate a hashed String for the given input string
			public static String md5(String input) {
		        
		        String md5 = null;
		         
		        if(null == input) return null;
		         
		        try {
		             
		        //Create MessageDigest object for MD5
		        MessageDigest digest = MessageDigest.getInstance("MD5");
		         
		        //Update input string in message digest
		        digest.update(input.getBytes(), 0, input.length());
		 
		        //Converts message digest value in base 16 (hex) 
		        md5 = new BigInteger(1, digest.digest()).toString(16);
		 
		        } catch (NoSuchAlgorithmException e) {
		 
		            e.printStackTrace();
		        }
		        return md5;
		    }
			
			public static String getSalt(){
				return UUID.randomUUID().toString();
			}
			
			/*
			 * this check to see if the input password is the same is the one stored in the database
			 * ARGUMENTS: the database password, database salt, and the user input password
			 * RETURNED: A boolean indicated if it is a match or not
			 */
			public static Boolean CheckPasswordCorrect(String dbPassword, String dbSalt, String inputPassword){
				if(dbPassword.equalsIgnoreCase( md5(inputPassword + dbSalt))){
					return true;
				}
				return false;
			}
}
